package com.star.learn.excel.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    @Value("${spring.rabbitmq.queue.name}")
    private String queueName;

    @Value("${spring.rabbitmq.queue.dlq-name}")
    private String dlqName;

    public static final String EXCHANGE = "student_exchange"; // 主数据交换机
    public static final String ROUTING_KEY = "student.routingKey"; // 主数据路由键
    public static final String DLX = "student_dlx_exchange"; // 死信交换机 (通用)
    public static final String DLQ_ROUTING_KEY = "student.routingKey.dlq"; // 死信路由键 (通用)

    // 正常队列 (主数据)
    @Bean
    public Queue studentQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", DLX); // 使用通用的 DLX
        args.put("x-dead-letter-routing-key", DLQ_ROUTING_KEY); // 使用通用的 DLQ 路由键
        return new Queue(queueName, true, false, false, args);
    }

    @Bean
    public DirectExchange studentExchange() {
        return new DirectExchange(EXCHANGE, true, false);
    }

    @Bean
    public Binding studentBinding() {
        return BindingBuilder.bind(studentQueue()).to(studentExchange()).with(ROUTING_KEY);
    }

    // 死信队列 (通用)
    @Bean
    public Queue studentDLQ() {
        return new Queue(dlqName, true);
    }

    @Bean
    public DirectExchange dlxExchange() {
        return new DirectExchange(DLX, true, false);
    }

    @Bean
    public Binding dlqBinding() {
        return BindingBuilder.bind(studentDLQ()).to(dlxExchange()).with(DLQ_ROUTING_KEY);
    }
}
